﻿#include "BeginAnimationScene.h"

BeginAnimationScene::BeginAnimationScene()
{
    //图片路径

    path_ = "fodder/pic/tank_main.png";
    pictureRect_ = {0, 0, 780, 780};
    //图片显示起始位置区域
    senceStartRect_ = {Game::GetInstance().GetWindowWidth(), Game::GetInstance().GetWindowHeight(), 780, 780};
     //图片显示终止位置区域
    senceEndRect_ = {0, 0, 780, 780};
    //动画播放时间,单位毫秒
    animationTime_ = 3000;
    //声音播放时间
    soundTime_ = 3000;
    //设置帧率,最大60
    fps_ = 50;
    //设置现在帧

}
BeginAnimationScene::~BeginAnimationScene()
{
}

void BeginAnimationScene::Init()
{
    allTimes_ = 0;
    currentFrame_=0;
    currentX_ =senceStartRect_.h;
    currentY_ = senceStartRect_.w;
    currentTexture_ = IMG_LoadTexture(Game::GetInstance().GetRenderer(), "fodder/pic/tank_main.png");
    if(currentTexture_ == nullptr)
    {
        std::cerr << "IMG_LoadTexture Error: " << IMG_GetError() << std::endl;
        return;
    }
    music_ = Mix_LoadMUS("fodder/sounds/intro.wav");
    if(music_ == nullptr)
    {
        std::cerr << "Mix_LoadMUS Error: " << Mix_GetError() << std::endl;
        return;
    }
    //播放音乐
    Mix_PlayMusic(music_, -1);
    int time = 3000;


}

void BeginAnimationScene::HandleEvent(SDL_Event *event)
{


}

UpDateResult BeginAnimationScene::Update(float deltaTime)
{
    allTimes_ += deltaTime;
    if(allTimes_>=animationTime_)
    {
        updateResult_.isSwitchScene = true;
        updateResult_.sceneType = SceneType::SceneBegin;
    }
    static float time=0;
    time+=deltaTime;
    if(currentX_>0)
    {
        if(time >= (1000 / 60))
        {
          currentX_ -= ((Game::GetInstance().GetWindowHeight()/(fps_*(animationTime_/1000))));
          currentY_-=((Game::GetInstance().GetWindowWidth()/(fps_*(animationTime_/1000))));
          time = 0;
        }
    
    }
   
       return  updateResult_;
}

void BeginAnimationScene::Render()
{
    auto renderer = Game::GetInstance().GetRenderer();
    SDL_RenderClear(renderer);
    senceCrrentRect_ = {currentX_,currentY_,780,780};
    SDL_RenderCopy(renderer, currentTexture_, &pictureRect_, &senceCrrentRect_);
    SDL_RenderPresent(renderer);
    SDL_RenderClear(renderer);
}
